package leetcom.com234;

import java.util.HashMap;
import java.util.List;

public class EX5714 {
    public String evaluate(String s, List<List<String>> knowledge) {
        HashMap<String, String> map = new HashMap<>();
        for (List<String> strings : knowledge) {
            map.put(strings.get(0),strings.get(1));
        }

        StringBuilder res = new StringBuilder();
        for (int i = 0; i <s.length() ; i++) {
            if(s.charAt(i)=='('){
                int first = i+1;
                while(s.charAt(i)!=')'){i++;}
                String temp = s.substring(first,i);

                if(!map.containsKey(temp)){
                    res.append('?');
                }else{
                    res.append(map.get(temp));
                }
            }else {
                res.append(s.charAt(i));
            }

        }
        return res.toString();
    }
}
